/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.bean;

import com.yunhoo.pay.model.Expense;
import com.yunhoo.sys.util.common.MoneyUtil;
import com.yunhoo.sys.util.common.Null;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

@ManagedBean(name = "printBean")
@SessionScoped
public class PrintBillBean {

    private Expense exp = null;

    public void PrintBillBean() {
        System.out.println("printBillBean init !");
    }

    public void process(OutputStream outStream, Object data) throws Exception {
        printLoanBill();
    }

    public void printLoanBill() throws Exception {
        System.out.println("exp : " + exp);
        if (exp == null) return;
        //设置参数
        Map map = new HashMap();//parameters
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("deptname", exp.getDept().getName());
        map.put("cpsn_name", exp.getUser().getCpsn_name());
        map.put("affair", exp.getAffair());
        map.put("costType", exp.getCostType());
        map.put("name", exp.getPayStyle().getName());
        map.put("ticketcode", exp.getTicketcode());
        map.put("paycode", exp.getPaycode());
        map.put("money", MoneyUtil.toChinese(String.valueOf(Null.convert(exp.getTotal() == null ? 0d : exp.getTotal()))));
        map.put("total", exp.getTotal());
        map.put("affixAmount", exp.getAffixAmount());

        ArrayList jasperPrintList = new ArrayList();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();

        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
        JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath("/page/pay/print/loan.jasper"), map);
        jasperPrintList.add(jasperPrint);
        JasperPrint jasperPrintBill = JasperFillManager.fillReport(scontext.getRealPath("/page/pay/print/bill.jasper"), map);
        jasperPrintList.add(jasperPrintBill);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRPdfExporter exporter = new JRPdfExporter();
        //exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        byte[] bytes = baos.toByteArray();

        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=\"relatorioPorData.pdf\"");
            response.setContentLength(bytes.length);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();
            outputStream.close();
        }
    }

    public Expense getExp() {
        return exp;
    }

    public void setExp(Expense exp) {
        this.exp = exp;
    }
}
